package com.webserver.http;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 响应对象
 * 该类的每一个实例用于表示服务端给客户端发送的一个HTTP响应内容,每个响应用三部分构成:
 * 状态行,响应头,响应正文
 */
public class HttpResponse {
    private int statusCode =200;//默认值200
    private String statusReason= "OK";//默认值OK

    //响应头相关信息
    Map<String ,String > headers=new HashMap<>();



    //响应正文相关信息
    private File entity;//响应正文对应的实体文件
    private Socket socket;
    public HttpResponse(Socket socket){
        this.socket=socket;
    }
    public void flush() throws IOException {
        //1发送状态行
        sendStatusLine();
        //2发送响应头
        sendHeaders();
        //3发送响应正文
        sendContent();

    }
    private void sendStatusLine() throws IOException {
        String line="HTTP/1.1 "+" "+statusCode+" "+statusReason;
        println(line);
    }
    private void sendHeaders() throws IOException {
            Set<Map.Entry<String, String>> enteySet=headers.entrySet();
        for(Map.Entry<String,String>e:enteySet){
            String name=e.getKey();//响应头的名字
            String value=e.getValue();//响应头对应的值
            String line=name+": "+value;
            println(line);
        }
        //单独度发送CRLF表示响应头发送完毕
        println("");
    }
    private void sendContent() throws IOException {
        try(
            FileInputStream fis=new FileInputStream(entity);
        ) {
            OutputStream out = socket.getOutputStream();
            byte[] data = new byte[1024 * 10];
            int len;
            while ((len = fis.read(data)) != -1) {
                out.write(data, 0, len);
            }
            System.out.println("响应发送结束");
        }//这里不写catch是因为异常抛出给流程控制来处理

    }
    private void println(String line) throws IOException {
        OutputStream out=socket.getOutputStream();
        //3.1发送状态行
        byte[]data=line.getBytes("ISO8859-1");
        out.write(data);
        out.write(13);
        out.write(10);
    }


    public int getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(int statusCode) {
        this.statusCode = statusCode;
    }

    public String getStatusReason() {
        return statusReason;
    }

    public void setStatusReason(String statusReason) {
        this.statusReason = statusReason;
    }

    public File getEntity() {
        return entity;
    }

    /**
     * 添加响应头正文对应的实体文件
     * 添加该文件的同时会根据实体文件自定添加两个说明正文的响应头:Content-Type和Content-Length
     * @param entity
     */
    public void setEntity(File entity) {
        this.entity = entity;
        //设置响应头
        //1根据entity获取该正文文件的文件名
        //2根据文件名获取到资源后缀名
        //3根据资源后缀名设置Content-Type的值
        String name=entity.getName();
        String []q=name.split("[.]");
//        Map<String ,String> hashmap=new  HashMap<>();
//        hashmap.put("html","text/html");
//        hashmap.put("css","text/css");
//        hashmap.put("js","application/javascript");
//        hashmap.put("png","image/png");
//        hashmap.put("gif","image/gif");
//        hashmap.put("jpg","image/jpeg");
        String type=HttpContext.getMineType(q[q.length-1]);
        System.out.println(q[q.length-1]+"的mime type:" +type);
        //3根据资源后缀名设置Content-type的值
       putHeader("Content-Type", type);
       putHeader("Content-Length",entity.length()+"");
    }

    /**
     * 添加一个要发送的的响应头
     * @param name
     * @param value
     */
    public void putHeader(String name,String value){
        this.headers.put(name,value);
    }
}







